package xcmg.device.web.marketingActivity;

import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import xcmg.device.dao.entity.marketing.PriceSearchReportDO;
import xcmg.device.dao.entity.company.CompanyDealerDO;
import xcmg.device.infra.AuthCode;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.crm.util.ActionResultUtil;
import xcmg.device.service.marketing.MarketingActivityService;
import xcmg.device.service.vo.FunctionResult;
import xcmg.device.service.vo.basic.MaterialsSearchVO;
import xcmg.device.service.vo.basic.MaterialsVO;
import xcmg.device.service.vo.marketing.*;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by sqq on 2018/3/1.
 */

@RestController
@RequestMapping("/marketingActivity")
public class MarketingActivityController extends BasicController {

    @Autowired
    private MarketingActivityService marketingActivityService;

    @RequestMapping(value = "/list",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult list() throws Exception{
        List<MarketingActivityVO> results=marketingActivityService.list(getOrgId());
        return actionValueResult(results);
    }

    @RequestMapping(value = "/enableList",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult enableList(@RequestParam("state") Integer state) throws Exception{
        List<MarketingActivityVO> results=marketingActivityService.enableList(state,getOrgId());
        return actionValueResult(results);
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.PriceScheme.ADD)
    public ActionResult add(@RequestBody MarketingActivityVO marketingActivityVO) throws Exception{
        marketingActivityVO.setOrgId(getOrgId());
        ErrorCode code=marketingActivityService.add(marketingActivityVO);
        return actionResult(code,marketingActivityVO.getId());
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.PriceScheme.EDIT)
    public ActionResult update(@RequestBody MarketingActivityVO marketingActivityVO){
        ErrorCode code=marketingActivityService.update(marketingActivityVO);
        return actionResult(code);
    }

    @RequestMapping(value = "/remove",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.PriceScheme.DEL)
    public ActionResult remove(@RequestBody MarketingActivityVO marketingActivityVO){
        ErrorCode code=marketingActivityService.remove(marketingActivityVO.getId());
        return actionResult(code);
    }

    @RequestMapping(value = "/marketingDealerList",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult marketingDealerList(String activityId)throws Exception{
        List<MarketingDealerVO> voList=marketingActivityService.marketingDealerList(getOrgId(),activityId);
        return actionValueResult(voList);
    }

    @RequestMapping(value = "/addDealer",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.PriceScheme.SET_CLIENT)
    public ActionResult addDealer(@RequestBody MarketingDealerAddVO marketingDealerAddVO) throws Exception{
        ErrorCode code=marketingActivityService.addDealer(marketingDealerAddVO,getOrgId());
        return actionResult(code);
    }

    @RequestMapping(value = "/saveOrRemoveDealer",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.PriceScheme.SET_CLIENT)
    public ActionResult saveOrRemoveDealer(@RequestBody MarketingDealerAddVO marketingDealerAddVO) throws Exception{
        ErrorCode code=marketingActivityService.saveOrRemoveDealer(marketingDealerAddVO,getOrgId());
        return actionResult(code);
    }

    @RequestMapping(value = "/marketingMaterialsList",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult marketingMaterialsList(@RequestBody SearchCommonVO<MaterialsSearchVO> searchVO)throws Exception{
        searchVO.getFilters().setOrgId(getOrgId());
        PageCommonVO<MaterialsVO> result=marketingActivityService.marketingMaterialsList(searchVO,getOrgId());
        return actionValueResult(result.getPageInfo());
    }

    @RequestMapping(value = "/saveMarketingMaterials",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.PriceScheme.SET_MATERIEL)
    public ActionResult saveMarketingMaterials(@RequestBody List<MarketingMaterialsAddVO> marketingMaterialsAddVOList) throws Exception{
        ErrorCode code=marketingActivityService.saveMarketingMaterials(marketingMaterialsAddVOList,getOrgId());
        return actionResult(code);
    }

    @RequestMapping(value = "/activityPriceList",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult activityPriceList(String dealerOrgId,String materialsNo,
                                          @RequestParam(required = false) String supplierOrgId)throws Exception{

        List<MarketingActivityPriceVO> results = new ArrayList<>();
        if (StringUtils.isNotEmpty(supplierOrgId)){
            results = marketingActivityService.activityPriceList(dealerOrgId, supplierOrgId, materialsNo);
        }else{
            results = marketingActivityService.activityPriceList(dealerOrgId, getOrgId(), materialsNo);
        }
        return actionValueResult(results);
    }

    @RequestMapping(value = "/activityPriceList2",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult activityPriceList2(String supplierOrgId, String materialsNo)throws Exception{
        List<MarketingActivityPriceVO> results=marketingActivityService.activityPriceList(getOrgId(),supplierOrgId,materialsNo);
        return actionValueResult(results);
    }

//    @RequestMapping(value = "/synSap",method = RequestMethod.GET)
//    @FastMappingInfo(needLogin = true,code = AuthCode.PriceScheme.SYN_PRICE)
//    public void synSap()throws Exception{
//        marketingActivityService.synSap(getOrgId(), 0);
//    }
//
//    @RequestMapping(value = "/synSapIncrement",method = RequestMethod.GET)
//    @FastMappingInfo(needLogin = true,code = AuthCode.PriceScheme.SYN_PRICE_INCRE)
//    public void synSapIncrement()throws Exception{
//        marketingActivityService.synSap(getOrgId(), 1);
//    }

    @RequestMapping(value = "/activityList",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult activityList(String dealerOrgId, Integer isDirectCast) throws Exception {
        if(dealerOrgId==null){
            return actionResult(null);
        }
        String orgId = getOrgId();
        if(Integer.valueOf(1).equals(isDirectCast) && marketingActivityService.isSecondProxy(dealerOrgId)) {
        	MarketingActivityVO activityVO=marketingActivityService.getDefaultMarketingActivity(dealerOrgId, orgId);
    		if(activityVO == null) {
    			return actionResult(ErrorCode.Success);
    		}
    		List<MarketingActivityVO> activityVOs = new ArrayList<MarketingActivityVO>();
    		activityVOs.add(activityVO);
    		return actionValueResult(activityVOs);
        }
        List<MarketingActivityVO> activityList=marketingActivityService.activityList(orgId, dealerOrgId);
        return actionValueResult(activityList);
    }

    @RequestMapping(value = "/exportAll",method = RequestMethod.POST)
    @ApiOperation("导出记录列表")
    @FastMappingInfo(needLogin = true,code = AuthCode.PriceScheme.SET_MATERIEL_EXPORT)
    public void exportAll(MaterialsSearchVO condition)throws Exception {
        if(condition == null){
            condition = new MaterialsSearchVO();
        }
        condition.setOrgId(getOrgId());
        marketingActivityService.exportAll(condition);
    }


    @ResponseBody
    @RequestMapping(value = "/import", method = RequestMethod.POST)
    @ApiOperation("价格方案批量导入零件")
    @FastMappingInfo(needLogin = true)
    public ActionResult importExcel(HttpServletRequest request) throws Exception {
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
        MultipartFile file = multipartRequest.getFile(multipartRequest.getFileNames().next());
        String fileName = file.getOriginalFilename();
        String orgId = request.getParameter("x-orgId");
        String activityId = request.getParameter("x-billId");

        if (StringUtils.isBlank(orgId)) {
            return actionResult(ErrorCode.IllegalArument);
        }
        FunctionResult<String> result = new FunctionResult<>();
        if (!fileName.trim().endsWith(".xls")) {
            result.setCode(ErrorCode.ImportExcelEndWithXLS);
        } else {

            InputStream in = file.getInputStream();
            result = marketingActivityService.importFormExcel(in, orgId, activityId);
        }
        return actionResult(result.getCode(), result.getValue());
    }


    @RequestMapping(value = "/download",method = RequestMethod.POST)
    @ApiOperation("下载价格方案零件导入模板")
    @FastMappingInfo(needLogin = true)
    public void download() throws Exception {
        marketingActivityService.download();
    }

    @RequestMapping(value = "/batchSaveDealer",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true,code = AuthCode.PriceScheme.SET_CLIENT)
    @ApiOperation("批量配置客户")
    public ActionResult batchSaveDealer(@RequestBody MarketingDealerAddVO marketingDealerAddVO) throws Exception{
        List<String> activityIdList = marketingDealerAddVO.getActivityIdList();
        if (null == activityIdList || activityIdList.isEmpty()){
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode code = marketingActivityService.batchSaveDealer(marketingDealerAddVO,getOrgId());
        return actionResult(code);
    }

    @RequestMapping(value = "/batchRemoveDealer",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    @ApiOperation("批量配置移除客户")
    public ActionResult batchRemoveDealer(@RequestBody MarketingDealerAddVO marketingDealerAddVO) throws Exception{
        List<String> activityIdList = marketingDealerAddVO.getActivityIdList();
        if (null == activityIdList || activityIdList.isEmpty()){
            return actionResult(ErrorCode.IllegalArument);
        }
        ErrorCode code = marketingActivityService.batchRemoveDealer(marketingDealerAddVO,getOrgId());
        return actionResult(code);
    }

    /**
     * @Des 代理商查看供应商价格方案
     * @Date 2020/12/8 15:04
     * @Author wangzhaoyu
     * @Param [paramVO]
     * @Return yb.ecp.fast.infra.infra.ActionResult
     */
    @PostMapping("/supplierPriceScheme")
    public ActionResult supplierPriceScheme(@RequestBody SearchCommonVO<SupplierPriceSchemeQeuryParamVO> condition) throws Exception {
        //判断查看供应商是不是平台

        SupplierPriceSchemeQeuryParamVO qeuryParm = condition.getFilters();
        if (qeuryParm == null || qeuryParm.getSupplierOrgId() == null) {
            return actionResult( ErrorCode.IllegalArument );
        }
        String orgId = getOrgId();
        condition.getFilters().setOrgId(orgId);
        CompanyDealerDO companyDealerDO = marketingActivityService.itemByOrgId( condition.getFilters().getSupplierOrgId(),orgId);
        if (companyDealerDO == null){
            return actionResult( ErrorCode.NotPlatform );
        }
        //
        String dealerOrgId = companyDealerDO.getDealerOrgId();
        qeuryParm.setDealerOrgId(dealerOrgId);
        PageCommonVO<SupplierPriceSchemeQueryResultVO> supplierPriceSchemeQueryResults = marketingActivityService.supplierPriceSchemeQuery(condition);
        return ActionResultUtil.successActionResult(supplierPriceSchemeQueryResults.getPageInfo());
    }

    @PostMapping("/priceSearchReportList")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("价格查询报表")
    public ActionResult priceSearchReportList(@RequestBody SearchCommonVO<PriceSearchReportDO> searchVO){
        PageCommonVO pageCommonVO = marketingActivityService.priceSearchReportList(searchVO);
        return actionValueResult(pageCommonVO);
    }

    @PostMapping("/priceSearchReportListClient")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("客户端价格查询报表")
    public ActionResult priceSearchReportListClient(@RequestBody SearchCommonVO<PriceSearchReportDO> searchVO){
        PageCommonVO pageCommonVO = marketingActivityService.priceSearchReportListForClient(searchVO);
        return actionValueResult(pageCommonVO);
    }

    @PostMapping("/priceExport")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("客户端价格查询导出")
    public ActionResult priceSearchReportExport(@RequestBody List<PriceSearchReportDO> reportDOS) throws Exception{
        marketingActivityService.invokeExport(reportDOS);
        return actionResult(ErrorCode.Success);
    }

    @PostMapping("/priceExportAll")
    @FastMappingInfo(needLogin = true)
    @ApiOperation("客户端价格查询导出全部")
    public ActionResult priceSearchReportExportAll(PriceSearchReportDO condition) throws Exception{
        marketingActivityService.priceSearchExportAll(condition);
        return actionResult(ErrorCode.Success);
    }

    @RequestMapping(value = "/listPriceGroup",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult listPriceGroup(String orgId) throws Exception{
        if (StringUtils.isBlank(orgId)) {
            return actionValueResult(new ArrayList<MarketingActivityVO>());
        }
        List<MarketingActivityVO> results=marketingActivityService.list(orgId);
        return actionValueResult(results);
    }

    @RequestMapping(value = "/activityList4Jck",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult activityList4Jck(String supplierOrgId, String receiveOrgId) throws Exception {
        if(supplierOrgId==null){
            return actionResult(null);
        }
        String orgId = getOrgId();
        if(StringUtils.isNotEmpty(receiveOrgId)) {
        	if(marketingActivityService.isSecondProxy(receiveOrgId)) {
        		MarketingActivityVO activityVO=marketingActivityService.getDefaultMarketingActivity(receiveOrgId, supplierOrgId);
        		if(activityVO == null) {
        			return actionResult(ErrorCode.Success);
        		}
        		List<MarketingActivityVO> activityVOs = new ArrayList<MarketingActivityVO>();
        		activityVOs.add(activityVO);
        		return actionValueResult(activityVOs);
        	}
        }
        List<MarketingActivityVO> activityList=marketingActivityService.activityList(supplierOrgId, orgId);
        return actionValueResult(activityList);
    }

}
